/**   
 * Copyright 2011 COMERE Harold
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package etrl.core;

import etrl.vecmath.Vector3f;
import etrl.vecmath.Vector4f;

/**
 * \class LightSource
 * \brief Class storing data of a light source§.
 * @author harold
 *
 */
public class LightSource
{
	/**
	 * Light position / direction. By convention
	 * If w = 1. the light is ponctual and x,y,z is its position
	 * If w = 0. the light is directional and x,y,z is its direction
	 */
	private Vector4f _data;
	
	/**
	 * Light color
	 */
	private Vector3f _color;
	
	/**
	 * \brief Initializes default light source
	 */
	public LightSource()
	{
		// Initialy the light is set as ponctual
		_data = new Vector4f(0.f, 0.f, 0.f, 1.f);
		
		// Initialy the light is not saturated white
		_color = new Vector3f(0.8f, 0.8f, 0.8f);
	}
	
	/**
	 * \brief Clones the light source.
	 * @return Cloned light source.
	 */
	public LightSource clone()
	{
		LightSource lightData = new LightSource();
		if(_data.w == 0.) lightData.setAsDirectional();
		if(_data.w == 1.) lightData.setAsPonctual();
		lightData.setPositionDirection(new Vector3f(_data.x, _data.y, _data.z));
		lightData.setColor(new Vector3f(_color));
		
		return lightData;
	}
	
	/**
	 * \brief Gets the light source position / orientation.
	 * @return Data of the light source.
	 */
	public Vector4f getData()
	{
		return new Vector4f(_data);
	}
	
	/**
	 * \brief Sets the light source position / direction.
	 * @param[in] positionDirection Position / direction to set.
	 */
	public void setPositionDirection(Vector3f positionDirection)
	{
		_data.x = positionDirection.x;
		_data.y = positionDirection.y;
		_data.z = positionDirection.z;
	}
	
	/**
	 * \brief Sets the light source as ponctual ( omnidirectional )
	 */
	public void setAsPonctual()
	{
		_data.w = 1.f;
	}
	
	/**
	 * \brief Sets the light source as directional
	 */
	public void setAsDirectional()
	{
		_data.w = 0.f;
	}
	
	/**
	 * \brief Gets the light source color.
	 * @return color.
	 */
	public Vector3f getColor()
	{
		return new Vector3f(_color);
	}
	
	/**
	 * \brief Defines the light source color.
	 * @param[in] color Color to set.
	 */
	public void setColor(Vector3f color)
	{
		_color.x = Math.min(color.x, 1.f);
		_color.y = Math.min(color.y, 1.f);
		_color.z = Math.min(color.z, 1.f);
	}
}
